package com.quanyan.ic.vendingMachine.http.controller;

import com.alibaba.druid.util.StringUtils;
import com.quanyan.ic.common.http.APIResponse;
import com.quanyan.ic.common.http.enums.ApiResponseEnum;
import com.quanyan.ic.vendingMachine.http.service.VmUploadService;
import org.apache.commons.collections4.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;

/**
 * Created by zhoujin on 2017/8/22.
 */
@Controller
@RequestMapping("/larkic/vendingMachine/log")
public class VmUploadLogController {
    @Autowired
    private VmUploadService vmUploadService;


    @RequestMapping(value = "/uploadLog",method = RequestMethod.POST)
    @ResponseBody
    public APIResponse getUploadInfo(@RequestParam(value="file",required=false) MultipartFile file,
                                     @RequestParam(value = "vmNumber") String vmNumber
    ) throws IllegalStateException,IOException {
        if(StringUtils.isEmpty(vmNumber)){
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_CANT_BE_EMPTY);
        }
        vmUploadService.upLoadLogFile(file,vmNumber);
        return APIResponse.returnSuccess();
    }

    @RequestMapping(value = "/getUploadLogMessage",method = RequestMethod.POST)
    @ResponseBody
    public APIResponse getUploadMessage(@RequestBody Map<String, Object> map){
        if(StringUtils.isEmpty(MapUtils.getString(map,"vmNumber")) || StringUtils.isEmpty(MapUtils.getString(map,"date")) ){
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_CANT_BE_EMPTY);
        }
        vmUploadService.sendUploadLogCmd(MapUtils.getString(map,"vmNumber"),MapUtils.getString(map,"date"));
        return APIResponse.returnSuccess();
    }

}
